package com.uls.web.controller.found;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.uls.common.core.domain.entity.SysUser;
import com.uls.found.domain.EduClaimRecord;
import com.uls.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.uls.common.annotation.Log;
import com.uls.common.core.controller.BaseController;
import com.uls.common.core.domain.AjaxResult;
import com.uls.common.enums.BusinessType;
import com.uls.found.domain.EduRecordRecruitment;
import com.uls.found.service.IEduRecordRecruitmentService;
import com.uls.common.utils.poi.ExcelUtil;
import com.uls.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 招领记录Controller
 * 
 * @author ruoyi
 * @date 2024-10-30
 */
@RestController
@RequestMapping("/found/recruitment")
public class EduRecordRecruitmentController extends BaseController
{
    @Autowired
    private IEduRecordRecruitmentService eduRecordRecruitmentService;

    @Autowired
    private ISysUserService iSysUserService;
    /**
     * 查询招领记录列表
     */
    @PreAuthorize("@ss.hasPermi('found:recruitment:list')")
    @GetMapping("/list")
    public TableDataInfo list(EduRecordRecruitment eduRecordRecruitment)
    {
        startPage();
        List<EduRecordRecruitment> list = eduRecordRecruitmentService.selectEduRecordRecruitmentList(eduRecordRecruitment);

        for (EduRecordRecruitment eduRecordRecruitment1:list){

            SysUser sysUser=iSysUserService.selectUserById(eduRecordRecruitment1.getStudentId());

             eduRecordRecruitment1.setUserName(sysUser.getUserName());
        }
        return getDataTable(list);
    }

    /**
     * 导出招领记录列表
     */
    @PreAuthorize("@ss.hasPermi('found:recruitment:export')")
    @Log(title = "招领记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EduRecordRecruitment eduRecordRecruitment)
    {
        List<EduRecordRecruitment> list = eduRecordRecruitmentService.selectEduRecordRecruitmentList(eduRecordRecruitment);
        ExcelUtil<EduRecordRecruitment> util = new ExcelUtil<EduRecordRecruitment>(EduRecordRecruitment.class);
        util.exportExcel(response, list, "招领记录数据");
    }

    /**
     * 获取招领记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('found:recruitment:query')")
    @GetMapping(value = "/{foundId}")
    public AjaxResult getInfo(@PathVariable("foundId") Long foundId)
    {
        return success(eduRecordRecruitmentService.selectEduRecordRecruitmentByFoundId(foundId));
    }

    /**
     * 新增招领记录
     */
    @PreAuthorize("@ss.hasPermi('found:recruitment:add')")
    @Log(title = "招领记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EduRecordRecruitment eduRecordRecruitment)
    {
        eduRecordRecruitment.setCreateBy(getUsername().toString());
        eduRecordRecruitment.setCreateTime(new Date());

        eduRecordRecruitment.setStudentId(getUserId());
        return toAjax(eduRecordRecruitmentService.insertEduRecordRecruitment(eduRecordRecruitment));
    }

    /**
     * 修改招领记录
     */
    @PreAuthorize("@ss.hasPermi('found:recruitment:edit')")
    @Log(title = "招领记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EduRecordRecruitment eduRecordRecruitment)
    {
        eduRecordRecruitment.setUpdateBy(getUsername().toString());
        eduRecordRecruitment.setUpdateTime(new Date());

        eduRecordRecruitment.setStudentId(getUserId());
        return toAjax(eduRecordRecruitmentService.updateEduRecordRecruitment(eduRecordRecruitment));
    }

    /**
     * 删除招领记录
     */
    @PreAuthorize("@ss.hasPermi('found:recruitment:remove')")
    @Log(title = "招领记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{foundIds}")
    public AjaxResult remove(@PathVariable Long[] foundIds)
    {

        int total=0;

        for (Long foundId:foundIds){

            EduRecordRecruitment eduRecordRecruitment = eduRecordRecruitmentService.selectEduRecordRecruitmentByFoundId(foundId);

            if (eduRecordRecruitment != null) {

                eduRecordRecruitment.setDeleteBy(getUsername().toString());

                eduRecordRecruitment.setDeleteTime(new Date());

                eduRecordRecruitment.setDelFlag(1);

                int a=eduRecordRecruitmentService.updateEduRecordRecruitment(eduRecordRecruitment);

                total=total+a;

            }
        }
        return toAjax(total);
    }


    //下载模板
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<EduRecordRecruitment> util = new ExcelUtil<EduRecordRecruitment>(EduRecordRecruitment.class);
        util.importTemplateExcel(response, "用户数据");
    }


    @Log(title = "招领管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('found:recruitment:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<EduRecordRecruitment> util = new ExcelUtil<EduRecordRecruitment>(EduRecordRecruitment.class);
        List<EduRecordRecruitment> eduRecordRecruitmentList = util.importExcel(file.getInputStream());
        String message = eduRecordRecruitmentService.importUser(eduRecordRecruitmentList, updateSupport);
        return success(message);
    }

}
